<?php
namespace app\finance\controller;
use think\Request;
use think\Exception;
use app\common\controller\Common;
class Refund extends Common
{
	protected $model = null;
	protected $order = 'rbooking_id desc';
	protected $afterActionList = [
        'addOperLog'  =>  ['only'=>'add,del'],
    ];

	public function _initialize()
	{
		parent::_initialize();
		$this->model = new \app\finance\model\Refund;
	}
	/**
	* 退款记账列表
	*/
	public function index()
	{
		if(Request()->isGet()){
			if(Request()->isAjax()){
				$data = Request()->param('');

				$page = isset($data['page']) ? $data['page'] : 1;
				$row = isset($data['limit']) ? $data['limit'] : $this->row;
				$search_arr = isset($data['keyword']) ? $data['keyword'] : '';
				$search = $this->keyword_search($search_arr);
				$order = $this->order;
				$where = $this->handleList($data);
				
				$res['list'] = $this->model->listData($where, $order, $page, $row, $search);
				$res['count'] = $this->model->allMoney($where, $order, $search);
				$count = $this->model->listCount($where, $order, $search);
				return $this->jsonList(0, config('status.SUCCESS_ONE'), $count, $res, $page, $row);
			}
			/*@供应商*/
			$supplier = model('pay')->getSupplier();
			$this->assign('supplier',$supplier);
			return $this->fetch('');
		}
	}
	/**
	* 新增退款记账
	*/
	public function add()
	{
		if(Request()->isGet()){
			$supplier = model('pay')->getSupplier();
			$this->assign('supplier',$supplier);
			/*@产品分类*/
			$gcate = model('staff/Category')->where('fid',0)->select();
			$this->assign('gcate',$gcate);
			return $this->fetch('');
		}
		if(Request()->isAjax()){
			$data = Request()->param('');
			$this->model->addtime = time();
			$cate_model = model('staff/Category');
			/*@产品分类名*/
			$data['cat_f_name'] = $cate_model->where('category_id',$data['category_id'])->value('category_name');
			/*@产品子分类名*/
			$data['cat_s_name'] = $cate_model->where('category_id',$data['cat_id'])->value('category_name');
			try{
				$this->model->allowField(true)->save($data);
				$this->log_id = $this->model->rbooking_id;
				$this->success(config('status.SUCCESS_ONE'));
			}catch(Exception $e){
				$this->error(config('status.ERROR_FIVE'));
			}
		}
	}
	/**
	* 删除退款记账
	*/
	public function del()
	{
		if(Request()->isAjax()){
			$data = Request()->param('');
			if(!isset($data['id']) || $data['id'] == ''){
				$this->error(config('status.ERROR_ONE'));
			}
			$this->log_id = $data['id'];
			try{
				$this->model->where('rbooking_id',$data['id'])->delete();
				$this->success(config('status.SUCCESS_ONE'));
			}catch(Exception $e){
				$this->error(config('status.ERROR_FIVE'));
			}
		}
	}

	/************************************ 私有 **************************************/

	/**
	* 退款记账-列表数据处理
	*/
	protected function handleList($data)
	{
		$where = [];

		/*@退款类型*/
		if(isset($data['ty']) && $data['ty'] != ''){
			$where['ty'] = $data['ty'];
		}
		/*@供应商*/
		if(isset($data['supplier_id']) && $data['supplier_id'] != ''){
			$where['supplier_id'] = $data['supplier_id'];
		}
		/*@时间*/
		if(isset($data['start']) && $data['start'] != '' && isset($data['end']) && $data['end'] != ''){
			if(strtotime($data['start']) > strtotime($data['end'])){
				return false;
			}
			$where['addtime'] = [
				['egt', strtotime($data['start'])],
				['lt', strtotime($data['end'])]
			];
		}else if(isset($data['start']) && $data['start'] != ''){
			$where['addtime'] = ['egt', strtotime($data['start'])];
		}else if(isset($data['end']) && $data['end'] != ''){
			$where['addtime'] = ['lt', strtotime($data['end'])];
		}
		return $where;
	}
}